import com.cyy.i.dao.IUserDao;
import com.cyy.i.dao.impl.UserDao;
import com.cyy.i.proxy.DynamicUserDaoProxy;
import com.cyy.i.proxy.StaticUserDaoProxy;
import com.cyy.ni.dao.CUserDao;
import com.cyy.ni.proxy.ProxyFactory;

public class Main {

    public static void staticProxy(){
        System.out.println("----静态代理----");
        IUserDao dao= new UserDao();

        //静态代理的实质是用代理类引用目标类的实例执行，将原来的办法做二层封装，实现原有事务执行前后的一些操作，即办法的重写，是事先编译好的，生成字节码的代理类。
        StaticUserDaoProxy daoProxy=new StaticUserDaoProxy(dao);
        //执行调用
        daoProxy.save();

    }
    public static void dynamicProxy(){
        System.out.println("----jdk动态代理----");
        IUserDao dao= new UserDao();

        System.out.println(dao.getClass());
        IUserDao daoProxy = (IUserDao) new DynamicUserDaoProxy(dao).getProxyInstance();
        System.out.println(daoProxy.getClass());
        //执行调用
        daoProxy.save();

    }

    public static void CGlibDynamicProxy(){

        System.out.println("----CGlib动态代理----");
        CUserDao target = new CUserDao();
        CUserDao proxy = (CUserDao)new ProxyFactory(target).getProxyInstance();
        System.out.println(proxy.getClass());
        proxy.save();

    }
    public static void main(String[] args) {
//        staticProxy();
        CGlibDynamicProxy();
//        dynamicProxy();
    }
}
